package com.zzl.mine.controller.admin.oss;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zzl.mine.controller.base.BaseController;
import com.zzl.mine.tool.ResponseResult;

/**
 * @description 阿里云OSS Controller
 * @author rambo
 * @date 2018年12月15日 下午1:43:39
 */
@RequestMapping("/admin/oss")
@Controller
public class OSSController extends BaseController {

	/**
	 * @description 文件上传
	 * @param request 请求对象
	 * @param file 上传文件对象
	 * @return 返回响应数据
	 */
	@RequestMapping(value = "/imgUpload", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> imgUpload(HttpServletRequest request, @RequestParam(value = "imgFile") MultipartFile imgFile) {
		Map<String, Object> value = new HashMap<String, Object>();
		value.put("success", true);
		value.put("errorCode", 0);
		value.put("errorMsg", "");
		try {
			String url = ossService.uploadImg(imgFile);
			value.put("data", url);
		} catch (Exception e) {
			e.printStackTrace();
			value.put("success", false);
			value.put("errorCode", 200);
			value.put("errorMsg", "文件上传失败");
		}
		return value;
	}
	
	/**
	 * @description 访问文件上传页面
	 * @return 返回逻辑视图地址
	 */
	@RequestMapping("/toImgUpload")
	public String toImgUpload() {
		return "oss/imgUpload";
	}
	
	/**
	 * @description 访问测试接收数组页面
	 * @return 返回逻辑视图地址
	 */
	@RequestMapping("/toTestArray")
	public String toTestArray() {
		return "oss/testArray";
	}
	
	/**
	 * @description 测试接收数组
	 * @param ids id数组
	 * @return 返回响应数据
	 */
	@ResponseBody
    @RequestMapping(value = "/test/array", method = RequestMethod.POST)
    public ResponseResult test(@RequestParam(value = "ids[]") String[] ids) {
        for (int i = 0 ; i < ids.length; i++) {
            System.out.println(ids[i]);
        }
        return ResponseResult.buildResult(200, "success", ids);
    }

}
